﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GBMS.APP_UI.Common;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_SendMessage : System.Web.UI.Page
    {
        private string type;
        private IProject projectBLL;
        private EmployeeBLL empBLL = new EmployeeBLL();
        private MessageSendBLL send = new MessageSendBLL();
        private Main main = new Main();
        private ComboBLL comboBLL = new ComboBLL();
        private SsMng ssMng = new SsMng();
        //private UserBLL userBLL = new UserBLL();
        //MessageSend.MessageSend ess = new MessageSend.MessageSend();

        protected void Page_Load(object sender, EventArgs e)
        {
            type = Request.QueryString["type"].ToString();
            projectBLL = Project_Factory.GetProjectFactory(type);

            if (!IsPostBack)
            {
                LoadDDLEmpSeq();
                DataTable dtb = send.GetCurrentApprover(ssMng.EmpSeqI, "bgm_request", 1);

                if (dtb != null)
                {
                    if (dtb.Rows.Count > 0)
                    {
                        SetEmpInfo(dtb.Rows[0]["emp_seq"].ToString());
                    }
                }
            }
        }

        protected void LoadDDLEmpSeq()
        {
            /*
            int? jobSiteHeadEmp = null;

            if (!String.IsNullOrEmpty(Request["jobsiteid"]))
            {
                var jobSiteHeadEmp = comboBLL.GetJobSiteHeadEmployee(Request["jobsiteid"]);
                if (jobSiteHeadEmp != null && (jobSiteHeadEmp["emp_seq"] as int?).HasValue)
                    ddlEmpSeq.SelectedValue = Convert.ToString((jobSiteHeadEmp["emp_seq"] as int?));
            }*/

            if (!String.IsNullOrEmpty(Request["jobsiteid"]))
            {
                var jobSiteHeadEmp = comboBLL.GetJobSiteHeadEmployee(Request["jobsiteid"]);
                DataTable dt;
                int? jobSiteHeadEmpSeq = null;

                if (jobSiteHeadEmp != null)
                {
                    jobSiteHeadEmpSeq = jobSiteHeadEmp["emp_seq"] as int?;
                    dt = empBLL.ListEmployeeByType("1", "");

                    if (jobSiteHeadEmpSeq.HasValue)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dr["sequence"] as int? == jobSiteHeadEmpSeq)
                                dr["full_name"] = dr["full_name"] + "*";
                        }

                        //// insert empty row
                        //DataRow dataRowEmpty = dt.NewRow();
                        //dataRowEmpty["misc"] = "---กรุณาเลือก---";
                        //dataRowEmpty["sequence"] = -1;
                        //dt.Rows.InsertAt(dataRowEmpty, 0); 
                    }
                }
                else
                {
                    dt = empBLL.ListEmployeeBySeq(null);
                }

                ddlEmpSeq.DataSource = dt;
                ddlEmpSeq.DataBind();

                ddlEmpSeq.SelectedValue = Convert.ToString(jobSiteHeadEmpSeq);
                SetEmpInfo(ddlEmpSeq.SelectedValue);
            }

        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string strToEmpSeq = lblEmpSeq.Text;

            if (String.IsNullOrEmpty(strToEmpSeq))
            {
                RadAjaxManager1.Alert("คำเตือน!! กรุณาเลือกผู้อนุมัติก่อนส่ง");
                return;
            }

            try
            {
                string strProjectID = Session["ProjectID"].ToString();
                MessageSendInfo info = new MessageSendInfo();
                info.send_date = DateTime.Today;
                info.ref_seq = main.ConvertNullInt(strProjectID);
                info.ref_type = "bgm_request";
                info.ref_no = "";
                info.send_text = "ส่งคำของบประมาณ : " + projectBLL.GetDataProjectBySequence(strProjectID).Rows[0]["bgm_prj_name"];
				info.from_emp_seq = main.ConvertNullInt(new SsMng().GetUssI(SsMng.SsUserType.EmpSeq));
                info.from_pos_seq = send.GetPosSeqFromEmpSeq(new SsMng().GetUssI(SsMng.SsUserType.EmpSeq));
                info.to_emp_seq = main.ConvertNullInt(strToEmpSeq);
                info.to_pos_seq = send.GetPosSeqFromEmpSeq(string.IsNullOrEmpty(strToEmpSeq) ? (int?)null : Convert.ToInt32(strToEmpSeq));
                info.from_send_seq = null;
                info.remarks = null;
                info.message_status = "0";
                info.approve_path_seq = 1;
				info.created_by = main.ConvertNullInt(new SsMng().GetUssI(SsMng.SsUserType.EmpSeq));
                info.created_when = DateTime.Now;

                projectBLL.ApproveProject(strProjectID, "P");
                send.Insert(info);
                RadAjaxManager1.Alert("บันทึกเรียบร้อยแล้ว");
                ClosePopup();
            }
            catch (Exception ex)
            { 
                RadAjaxManager1.Alert("เกิดข้อผิดพลาดในการบันทึกข้อมูล");
            }            
        }

        protected void SetEmpInfo(string strEmpSeq)
        {
            if (!String.IsNullOrEmpty(strEmpSeq))
            {
                // Load that employee
                DataTable dtb = empBLL.ListEmployeeBySeq(strEmpSeq);

                lblEmpSeq.Text = dtb.Rows[0]["sequence"].ToString();
                ddlEmpSeq.Text = dtb.Rows[0]["full_name"].ToString();
                lblPosName.Text = dtb.Rows[0]["pos_name"].ToString();
                lblClassName.Text = dtb.Rows[0]["class_name"].ToString();
                lblExecPosName.Text = dtb.Rows[0]["exec_pos_name"].ToString();
                lblExecPosName.Text = String.IsNullOrEmpty(lblExecPosName.Text) ? "-" : lblExecPosName.Text;
                lblJobsiteName.Text = dtb.Rows[0]["job_site_name"].ToString();
            }
        }

        protected void ClosePopup()
        {
            ScriptManager.RegisterClientScriptBlock(
                this.Page,
                this.GetType(),
                "WebUserControlSript",
                "returnToParent()",
                true);
        }

        protected void ddlEmpSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            SetEmpInfo(ddlEmpSeq.SelectedValue);
        }

        protected void ddlEmpSeq_DataBound(object sender, EventArgs e)
        {
            //set the initial footer label
            ((Literal)ddlEmpSeq.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(ddlEmpSeq.Items.Count);
        }

        protected void ddlEmpSeq_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            ddlEmpSeq.DataSource = empBLL.ListEmployeeByType(string.Empty, "");
            ddlEmpSeq.DataBind();
        }

        protected void ddlEmpSeq_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = ((DataRowView)e.Item.DataItem)["full_name"].ToString();
            e.Item.Value = ((DataRowView)e.Item.DataItem)["sequence"].ToString();
        }
    }
}
